import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../../config/lf_route.dart';

toBrowserPage({
  required String title,
  required String url,
}) {
  Get.toNamed(LFRoute.pathBrowser, arguments: {
    'title': title,
    'url': url,
  });
}

class LfBrowserLogic extends GetxController {
  late String url;
  late String title;
  RxBool isLoading = false.obs;
  late final WebViewController controller;
  @override
  void onInit() {
    url = Get.arguments['url'] as String;
    title = Get.arguments['title'] as String;
    const PlatformWebViewControllerCreationParams params =
        PlatformWebViewControllerCreationParams();
    controller = WebViewController.fromPlatformCreationParams(params);
    controller
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(Colors.white)
      ..setNavigationDelegate(NavigationDelegate(
        onProgress: (progress) {
          isLoading.value = true;
        },
        onWebResourceError: (error) {
          isLoading.value = false;
        },
        onPageFinished: (url) {
          isLoading.value = false;
        },
      ))
      ..loadRequest(Uri.parse(url));
    super.onInit();
  }
}
